﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using DevExpress.XtraReports.UI;

namespace Baocao
{
    public partial class BaoCao_THB : System.Web.UI.Page
    {
        private SqlConnection appConn;
        private System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("vi-VN");
        public static string TenCty = "CÔNG TY CỔ PHẦN HIVI", DcCty = "ĐC:38 - Trường Chinh - Hà Nội", DtCty = "ĐT:(04)32343242", Loaipm = "Phần mềm bán hàng", Tenpm = "HIVI 2S PRO Mobie", Webpm = "www.hivi.com.vn";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["appConn"] != null)
            {
                appConn = (SqlConnection)Session["appConn"];
                if (!IsPostBack)
                {
                    Baocao.reportpage yourMaster = (Baocao.reportpage)this.Master;
                    yourMaster.ChangeTitle("Báo cáo tổng hợp hàng bán");

                    dttuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    dtdenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");

                    drnhomhang.DataSource = getNhomHang();
                    drnhomhang.DataValueField = "Nhvt2ID";
                    drnhomhang.DataTextField = "Nhvt2Name";
                    drnhomhang.DataBind();

                    drtennv.DataSource = getNhanVien();
                    drtennv.DataValueField = "BpkdID";
                    drtennv.DataTextField = "BpkdName";
                    drtennv.DataBind();

                    drnhomvuviec.DataSource = getNhomVuViec();
                    drnhomvuviec.DataValueField = "NhvvID";
                    drnhomvuviec.DataTextField = "NhvvName";
                    drnhomvuviec.DataBind();
                    txtlbTenNv.Text = drtennv.SelectedItem.Value;
                }
            }
            else
            {
                Response.Redirect("~\\Login.aspx");
            }

        }


        protected void btIn_Click(object sender, EventArgs e)
        {
            string ngaybd_bc = "", ngaykt_bc = "", ma_doitac = "", ma_bpkd = "", ma_kho = "", nhom_vt = "", ma_vt = "", ma_vv = "", ngaybd = "", ma_nhvv = "", ngaykt = "", ma_khl = "";
            try
            {
                if (!dttuNgay.Text.ToString().Equals(""))
                {
                    ngaybd_bc = DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("yyyyMMdd");
                    ngaybd = DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("dd/MM/yyyy");
                }
                if (!dtdenNgay.Text.ToString().Equals(""))
                {
                    ngaykt_bc = DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("yyyyMMdd");
                    ngaykt = DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("dd/MM/yyyy");
                }
                ma_doitac = txtMaKhHang.Text.Trim();
                ma_bpkd = drtennv.SelectedValue;
                nhom_vt = drnhomhang.SelectedValue;
                ma_vt = txtMaHangHoa.Text.Trim();
                ma_nhvv = drnhomvuviec.SelectedValue;
                ma_vv = drvuviec.SelectedValue;
                DataSet ds = new DataSet();

                if (drmauin.SelectedValue == "R0073")
                {
                    string s = "EXEC [fs_HG_BCTHHB]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', '" + ma_doitac + "','" + ma_khl + "',  '" + ma_bpkd + "','" + ma_kho + "',  '" + nhom_vt + "','" + ma_vt + "','" + ma_vv + "', '" + ma_nhvv + "' ";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);
                    DataTable dt = ds.Tables["bc"].Copy();
                    dt.Columns.Add("Stt", typeof(string));
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i]["Stt"] = "" + (i + 1);
                    }
                    dt.AcceptChanges();

                    rptTHB_Sl rpt = new rptTHB_Sl();
                    rpt.DataSource = dt;

                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";

                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;
                    Session["Report"] = rpt;
                }

                if (drmauin.SelectedValue == "R0047")
                {
                    string s = "EXEC [fs_HG_BCTHHBDT]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', '" + ma_doitac + "','" + ma_khl + "',  '" + ma_bpkd + "','" + ma_kho + "',  '" + nhom_vt + "','" + ma_vt + "','" + ma_vv + "', '" + ma_nhvv + "' ";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);
                    DataTable dt = ds.Tables["bc"];

                    XtraReport rpt;
                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        rpt = new m_rptTHB_SlDt();
                    }
                    else
                    {
                        rpt = new rptTHB_SlDt();
                    }
                    rpt.DataSource = dt;

                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";

                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;
                    Session["Report"] = rpt;
                }

                if (drmauin.SelectedValue == "R0072")
                {
                    string s = "EXEC [fs_HG_BCTHHBDTLL]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', '" + ma_doitac + "','" + ma_khl + "',  '" + ma_bpkd + "','" + ma_kho + "',  '" + nhom_vt + "','" + ma_vt + "','" + ma_vv + "', '" + ma_nhvv + "' ";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);
                    DataTable dt = ds.Tables["bc"];
                    XtraReport rpt;
                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        rpt = new m_rptTHB_SlDtLl();
                    }
                    else
                    {
                        rpt = new rptTHB_SlDtLl();
                    }
                    rpt.DataSource = dt;

                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";

                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;
                    Session["Report"] = rpt;
                }

                if (drmauin.SelectedValue == "R0106")
                {
                    string s = "EXEC [fs_HG_BCTHHBDTNTKH]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', '" + ma_doitac + "','" + ma_khl + "',  '" + ma_bpkd + "','" + ma_kho + "',  '" + nhom_vt + "','" + ma_vt + "','" + ma_vv + "', '" + ma_nhvv + "' ";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);
                    DataTable dt = ds.Tables["bc"].Copy();

                    decimal tongtienhang = Convert.ToDecimal(dt.Rows[0]["Doanh_thu"].ToString());
                    decimal TongCk = Convert.ToDecimal(dt.Rows[1]["Doanh_thu"].ToString());
                    decimal Tongtien = Convert.ToDecimal(dt.Rows[2]["Doanh_thu"].ToString());

                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();

                    XtraReport rpt;
                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        rpt = new m_rptTHB_SlDtKh();
                    }
                    else
                    {
                        rpt = new rptTHB_SlDtKh();
                    }
                    rpt.DataSource = dt;

                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";

                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;

                    rpt.Parameters["Tongtienhang"].Value = tongtienhang;
                    rpt.Parameters["TongCk"].Value = TongCk;
                    rpt.Parameters["Tongtien"].Value = Tongtien;

                    Session["Report"] = rpt;
                }

                if (drmauin.SelectedValue == "R0107")
                {
                    string s = "EXEC [fs_HG_BCTHHBDTNTNV]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', '" + ma_doitac + "','" + ma_khl + "',  '" + ma_bpkd + "','" + ma_kho + "',  '" + nhom_vt + "','" + ma_vt + "','" + ma_vv + "', '" + ma_nhvv + "' ";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);
                    DataTable dt = ds.Tables["bc"].Copy();

                    decimal tongtienhang = Convert.ToDecimal(dt.Rows[0]["Doanh_thu"].ToString());
                    decimal TongCk = Convert.ToDecimal(dt.Rows[1]["Doanh_thu"].ToString());
                    decimal Tongtien = Convert.ToDecimal(dt.Rows[2]["Doanh_thu"].ToString());

                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                     XtraReport rpt;
                     if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                     {
                         rpt = new m_rptTHB_SlDtNV();
                     }
                     else
                     {
                         rpt = new rptTHB_SlDtNV();
                     }
                    rpt.DataSource = dt;

                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";

                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;

                    rpt.Parameters["Tongtienhang"].Value = tongtienhang;
                    rpt.Parameters["TongCk"].Value = TongCk;
                    rpt.Parameters["Tongtien"].Value = Tongtien;

                    Session["Report"] = rpt;
                }

                if (drmauin.SelectedValue == "R0108")
                {
                    string s = "EXEC [fs_HG_BCTHHBDTNTKHO]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', '" + ma_doitac + "','" + ma_khl + "',  '" + ma_bpkd + "','" + ma_kho + "',  '" + nhom_vt + "','" + ma_vt + "','" + ma_vv + "', '" + ma_nhvv + "' ";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);
                    DataTable dt = ds.Tables["bc"].Copy();

                    decimal tongtienhang = Convert.ToDecimal(dt.Rows[0]["Doanh_thu"].ToString());
                    decimal TongCk = Convert.ToDecimal(dt.Rows[1]["Doanh_thu"].ToString());
                    decimal Tongtien = Convert.ToDecimal(dt.Rows[2]["Doanh_thu"].ToString());

                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                    XtraReport rpt;
                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        rpt = new m_rptTHB_SlDtKho();
                    }
                    else
                    {
                        rpt = new rptTHB_SlDtKho();
                    }
                    rpt.DataSource = dt;

                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";

                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;

                    rpt.Parameters["Tongtienhang"].Value = tongtienhang;
                    rpt.Parameters["TongCk"].Value = TongCk;
                    rpt.Parameters["Tongtien"].Value = Tongtien;

                    Session["Report"] = rpt;
                }

                if (drmauin.SelectedValue == "R0109")
                {
                    string s = "EXEC [fs_HG_BCTHHBDTNTNH]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', '" + ma_doitac + "','" + ma_khl + "',  '" + ma_bpkd + "','" + ma_kho + "',  '" + nhom_vt + "','" + ma_vt + "','" + ma_vv + "', '" + ma_nhvv + "' ";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);
                    DataTable dt = ds.Tables["bc"].Copy();

                    decimal tongtienhang = Convert.ToDecimal(dt.Rows[0]["Doanh_thu"].ToString());
                    decimal TongCk = Convert.ToDecimal(dt.Rows[1]["Doanh_thu"].ToString());
                    decimal Tongtien = Convert.ToDecimal(dt.Rows[2]["Doanh_thu"].ToString());

                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                    dt.Rows.RemoveAt(0);
                    dt.AcceptChanges();
                    XtraReport rpt;
                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        rpt = new m_rptTHB_SlDtNhHang();
                    }
                    else
                    {
                        rpt = new rptTHB_SlDtNhHang();
                    }
                    rpt.DataSource = dt;

                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";

                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;

                    rpt.Parameters["Tongtienhang"].Value = tongtienhang;
                    rpt.Parameters["TongCk"].Value = TongCk;
                    rpt.Parameters["Tongtien"].Value = Tongtien;

                    Session["Report"] = rpt;
                }

                if (drmauin.SelectedValue == "R0124")
                {
                    string s = "EXEC [fs_HG_BCTHHBDTNTN]  '" + ngaybd_bc + "', '" + ngaykt_bc + "', '" + ma_doitac + "','" + ma_khl + "',  '" + ma_bpkd + "','" + ma_kho + "',  '" + nhom_vt + "','" + ma_vt + "','" + ma_vv + "', '" + ma_nhvv + "' ";
                    hg3.hg3.sql.SQLRetrieve(appConn, s, "bc", ref ds);
                    DataTable dt = ds.Tables["bc"].Copy();
                    XtraReport rpt;
                    if (Session["Mobie"] != null && Session["Mobie"].ToString() == "1")
                    {
                        rpt = new m_rptTHB_SlDtLlNgay();
                    }
                    else
                    {
                        rpt = new rptTHB_SlDtLlNgay();
                    }
                    rpt.DataSource = dt;

                    rpt.Parameters["tungay"].Value = ngaybd;
                    rpt.Parameters["denngay"].Value = ngaykt;
                    rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";

                    rpt.Parameters["tenCty"].Value = TenCty;
                    rpt.Parameters["dcCty"].Value = DcCty;
                    rpt.Parameters["dtCty"].Value = DtCty;

                    rpt.Parameters["Tenpm"].Value = Tenpm;
                    rpt.Parameters["Loaipm"].Value = Loaipm;
                    rpt.Parameters["Webpm"].Value = Webpm;

                    Session["Report"] = rpt;
                }


                Response.Redirect("~\\Print.aspx");
            }
            catch (Exception ex)
            {
                return;
            }
        }

        protected void drnhomhang_SelectedIndexChange(object sender, EventArgs objec)
        {
            txtMaHangHoa.Text = "";
            txtTenHangHoa.Text = "";
        }

        protected void drtennv_SelectedIndexChange(object sender, EventArgs objec)
        {
            txtlbTenNv.Text = drtennv.SelectedItem.Value;
        }

        protected void drnhomvuviec_SelectedIndexChange(object sender, EventArgs objec)
        {
            DataSet ds = new DataSet();
            if (drnhomvuviec.SelectedValue != "")
                hg3.hg3.sql.SQLRetrieve(appConn, "SELECT vvID, vvName FROM dmvv WHERE Status = '1' and FK_NhvvID='" + drnhomvuviec.SelectedValue + "'", "dmvv", ref ds);
            else
                hg3.hg3.sql.SQLRetrieve(appConn, "SELECT vvID, vvName FROM dmvv WHERE Status = '1'", "dmvv", ref ds);
            DataTable dt = ds.Tables["dmvv"];
            DataRow newR = dt.NewRow();
            newR["vvID"] = "";
            newR["vvName"] = "Tất cả";
            dt.Rows.Add(newR);
            drvuviec.DataSource = dt;
            drvuviec.DataTextField = "vvName";
            drvuviec.DataValueField = "vvID";
            drvuviec.DataBind();
        }

        private DataTable getNhomHang()
        {
            DataSet ds = new DataSet();
            hg3.hg3.sql.SQLRetrieve(appConn, "SELECT Nhvt2ID, Nhvt2Name FROM dmnhvt2 WHERE Status = '1'", "dmnhvt2", ref ds);
            DataTable dt = ds.Tables["dmnhvt2"];
            DataRow newR = dt.NewRow();
            newR["Nhvt2ID"] = "";
            newR["Nhvt2Name"] = "Tất cả";
            dt.Rows.Add(newR);
            return dt;
        }

        private DataTable getNhanVien()
        {
            DataSet ds = new DataSet();
            hg3.hg3.sql.SQLRetrieve(appConn, "SELECT BpkdID, BpkdName FROM dmbpkd WHERE Status = '1'", "dmbpkd", ref ds);
            DataTable dt = ds.Tables["dmbpkd"];
            DataRow newR = dt.NewRow();
            newR["BpkdID"] = "";
            newR["BpkdName"] = "Tất cả";
            dt.Rows.Add(newR);
            return dt;
        }

        private DataTable getNhomVuViec()
        {
            DataSet ds = new DataSet();
            hg3.hg3.sql.SQLRetrieve(appConn, "SELECT NhvvID, NhvvName FROM dmnhvv WHERE Status = '1'", "dmnhvv", ref ds);
            DataTable dt = ds.Tables["dmnhvv"];
            DataRow newR = dt.NewRow();
            newR["NhvvID"] = "";
            newR["NhvvName"] = "Tất cả";
            dt.Rows.Add(newR);
            return dt;
        }

    }
}
